{template 'common/header'}
<ul class="nav nav-tabs">
	<li class="active"><a href="{php echo $this->createWebUrl('item',array('op' =>'display'))}">商品管理</a></li>
	<li><a href="{php echo $this->createWebUrl('item',array('op' =>'create'))}">添加商品</a></li>
</ul>
<div class="panel-body">
	<div class="row">
		<div class="col-sm-12 col-md-8">
			<form action="{php echo $this->createWebUrl('item')}" method="post" class="form-inline" role="form">
				<div class="form-group">
					<label>商品搜索</label>
					<input class="form-control" name="keyword" placeholder="请输入商品名称" type="text" value="{$keyword}"  />
				</div>
				<button class="btn btn-default"><i class="fa fa-search"></i> 搜索</button>
			</form>
		</div>
		<div class="col-sm-12 col-md-4">
			<a class="btn btn-primary pull-right" href="{php echo $this->createWebUrl('item',array('op' => 'create'));}">添加商品</a>
		</div>
	</div>
	<div class="we7-margin-top-sm">
		<table class="table table-bordered we7-form we7-table table-hover vertical-middle">
			<tbody>
				<tr>
					<th style="width:3%;">
						<input type="checkbox" name="check" id="id-0" class="check_all" />
						<label for="id-0">&nbsp;</label>
					</th>
					<th style="width:5%;">商品ID</th>
					<th class="text-center" style="width:15%;">商品名称</th>
					<th class="text-center" style="width:15%;">商品图片</th>
					<th class="text-center" style="width:15%;">所需积分</th>
					<th class="text-center" style="width:15%;">已兑换</th>
					<th class="text-center" style="width:15%;">库存</th>
	                <th class="text-center" style="width:8%;">上架状态</th>
	                <th class="text-center" style="width:20%;">管理操作</th>
				</tr>
			</tbody>
			{loop $list $row}
			{php $photos = json_decode($row['photos'],true);}
			<tr>
				<td style="overflow:hidden;">
					<input type="checkbox" id="id-{$row['item_id']}" name="check" value="{$row['item_id']}" />
					<label for="id-{$row['item_id']}">&nbsp;</label>
				</td>
				<td>{$row['item_id']}</td>
				<td class="text-center">{$row['title']}</td>
				<td class="text-center">
					<img src="{$_W['attachurl']}{$photos[0]}" style="width:40px;" />
				</td>
				<td class="text-center">{$row['price']}</td>
				<td class="text-center">{$row['sold']}</td>
				<td class="text-center">{$row['stock']}</td>
				<td class="text-center">
					{if $row['is_sale'] == 1}<span class="label label-success">正常</span>{/if}
					{if $row['is_sale'] == 0}<span class="label label-warning">下架</span>{/if}
				</td>
	            <td class="text-center">
	                <a class="btn btn-default btn-sm" href="{php echo $this->createWebUrl('item', array('op' => 'edit', 'id' => $row['item_id']))}" title="编辑"><i class="fa fa-pencil"></i></a>
	                <a class="btn btn-default btn-sm btn-delete" data-href="{php echo $this->createWebUrl('item', array('op' => 'delete', 'id' => $row['item_id']))}" title="删除"><i class="fa fa-times"></i></a>
	            </td>
			</tr>
			{/loop}
			<tr>
				<td colspan="9">
					<div class="row">
						<div class="col-xs-12 text-left">
							<input name="batch" type="submit" data-cmd="del" data-text="确认要删除这些商品吗，删除后将不可恢复?" class="btn btn-danger" value="批量删除" />
							<input name="batch" type="submit" data-cmd="open"  data-text="确定要上架这些商品吗?" class="btn btn-success" value="批量上架" />
							<input name="batch" type="submit" data-cmd="close"  data-text="确定要下架这些商品吗?" class="btn btn-warning" value="批量下架" />
							<input type="hidden" name="token" value="{$_W['token']}" />
						</div>
					</div>
				</td>
			</tr>
		</table>
		{if empty($list)}
			<span style="margin-left: 45%;">暂无相关数据</span><br />
		{/if}
		<tr>
			<div class="pull-right">{$pager}</div>
       </tr>
	</div>
</div>
<script>
	$(".check_all").click(function() {
		$("[type='checkbox']").each(function() {
			if($(this).prop("checked")) {
				$(this).attr("checked", false);
				$(".check_all").prop("checked", false);
			} else {
				if($(this).attr("disabled") != "disabled") {
					$(this).prop("checked", true);
					$(".check_all").prop("checked", true);
				}
			}
		});
	});
	// 批量删除
	$("input[name=batch]").click(function() {
		var check = $("input[type=checkbox][class!=check_all]:checked");
		if(check.length < 1) {
			util.message('确认要修改选择要操作的数据！');
			return false;
		}
		var cmd = $(this).data("cmd");
		var text = $(this).data("text");
		util.confirm(function (){
			var id = new Array();
			check.each(function(i) {
				id[i] = $(this).val();
			});
			var url = "{php echo $this->createWebUrl('item', array('op' => 'batch'))}&cmd=" + cmd;
			$.post(url, {
				'idArr': id
			}, function(data) {
				util.message(data.data);
				top.location.reload();
			}, 'json');
		}, function () {
			return false;
		}, text);
	});
	// 单一删除
	$(".btn-delete").click(function(){
		var url = $(this).data("href");
		util.confirm(function () {
			location.href = url;
		}, function () {
			return false;
		}, '删除后该商品不可恢复，确定删除吗?');
	});
</script>
{template 'common/footer'}